// ==UserScript==
// @name        Google Play Books Downloader
// @description Download books you've uploaded to Google Play
// @version     1.0.2
// @author      Lari Rasku <lari.o.rasku@gmail.com>
// @license     MIT
// @homepageURL https://code.google.com/p/google-mybooks-user-js
// @supportURL  https://code.google.com/p/google-mybooks-user-js/issues
// @namespace   Google
// @match       https://play.google.com/books*
// @grant       none
// ==/UserScript==

// Copyright (c) 2014-5 Lari Rasku <lari.o.rasku@gmail.com>
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

"use strict"

function appendDownloadLink(bfeMenu) {
	// Assume that, when appendDownloadLink(bfeMenu) is called, only the
	// cover menu button element corresponding to `bfeMenu` bears the
	// following class signature.
	var query = ".bfe-volume-menu-button-focused"
	            ".bfe-volume-menu-button-open"
	var cardContent = document.querySelector(query).parentNode.parentNode
	var coverOverlay = cardContent.querySelector(".bfe-cover-overlay")
	var id = coverOverlay.href.match(/&id=([^&]+)/)[1]
	var download = document.createElement("a")
	download.href = "https://books.google.com/books/download?id=" + id
	              + "&output=uploaded_content"
	download.textContent = "Download"
	download.style.color = "#333"
	var div = document.createElement("div")
	div.className = "goog-menuitem"
	div.setAttribute("onmouseover",
		"this.style.backgroundColor = '#F6F6F6'")
	div.setAttribute("onmouseout",
		"this.style.backgroundColor = null")
	div.appendChild(download)
	bfeMenu.appendChild(div)
}

new MutationObserver(function (mutations) {
	for(var i = 0, mutation; mutation = mutations[i]; ++i) {
		for(var j = 0, node; node = mutation.addedNodes[j]; ++j) {
			// The popup menus of uploaded books contain only the
			// "Delete" entry, so we assume that every one-child
			// element of the `bfe-menu` class is the popup menu
			// of an uploaded book.
			if(node.classList.contains("bfe-menu")
			&& node.childElementCount === 1)
				appendDownloadLink(node)
		}
	}
}).observe(document.body, { childList: true })
